package cn.chenwx.bbsforum.service.Impl;

import cn.chenwx.bbsforum.entity.User;
import cn.chenwx.bbsforum.mapper.UserMapper;
import cn.chenwx.bbsforum.service.UserService;
import cn.chenwx.bbsforum.utils.BlogJSONResult;
import cn.chenwx.bbsforum.utils.DateUtils;
import cn.chenwx.bbsforum.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAllUser(int pageNum, int rows) {
        PageHelper.startPage(pageNum,rows);
        List<User> users=userMapper.findAllUser();
        for (User user:users) {
            if (user.getCreatTime()!=null){
                user.setCreatTimeStr( DateUtils.dateToString(user.getCreatTime(),"yyyy-MM-dd HH:mm"));
            }
        }
        return users;
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public BlogJSONResult addUser(User user) {
        user.setRole("ROLE_USER");// 用户默认是user权限
        user.setSex(1);// 用户默认是男生
        userMapper.addUser(user);
        return BlogJSONResult.ok();
    }

    @Override
    public int modifyUser(User user,int id) {
        return userMapper.modifyUser(user,id);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User findUserByName(String name) {
//        User user =userMapper.findUserByName(name);
//        if (user.getCreatTime()!=null){
//            user.setCreatTimeStr(DateUtils.dateToString(user.getCreatTime(),"yyyy-MM-dd HH:mm"));
//        }
        return userMapper.findUserByName(name);
    }

    @Override
    public int getUserNum() {
        return userMapper.getUserNum();
    }

    @Override
    public BlogJSONResult updateUserStatus(int status, int id) {
       userMapper.updateUserStatus(status,id);
       return BlogJSONResult.ok();
    }

    @Override
    public BlogJSONResult updateUser(User user, int id) {
         userMapper.updateUser(user,id);
         return BlogJSONResult.ok();
    }

    @Override
    public void updateAvatarImgUrlById(String avatarImgUrl, int id) {
        userMapper.updateAvatarImgUrlById(avatarImgUrl, id);
    }

    public BlogJSONResult getHeadPortraitUrl(int id) {
        String avatarImgUrl = userMapper.getHeadPortraitUrl(id);
        return BlogJSONResult.ok(avatarImgUrl);
    }

    @Override
    public BlogJSONResult getUserPersonalInfoByUsername(String username) {

        User user = userMapper.getUserPersonalInfoByUsername(username);
        if (user.getCreatTime()!=null){
            user.setCreatTimeStr(DateUtils.dateToString(user.getCreatTime(),"yyyy-MM-dd"));
        }
        return BlogJSONResult.ok(user);
    }

    @Override
    public boolean usernameIsExist(String username) {
        User user = userMapper.findUsernameByUsername(username);
        return user != null;
    }

    @Override
    public BlogJSONResult savePersonalDate(User user, String username) {
        user.setUsername(user.getUsername().trim().replaceAll(" ", StringUtil.BLANK));
        String newName = user.getUsername();
        if(newName.length() > StringUtil.USERNAME_MAX_LENGTH) {
            return BlogJSONResult.build(1,"用户名太长",null);
        }else if (StringUtil.BLANK.equals(newName)){
            return BlogJSONResult.build(2,"用户名不能为空",null);
        }

        int status;
        //改了昵称
        if(!newName.equals(username)){
            if(usernameIsExist(newName)){
                return BlogJSONResult.build(3,"用户名已存在",null);
            }
            status = 503;
            //注销当前登录用户

        }
        //没改昵称
        else {
            status = 504;
        }
        if (user.getCreatTimeStr()!=null){
            try {
                user.setCreatTime(DateUtils.stringToDate(user.getCreatTimeStr(),"yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        userMapper.savePersonalDate(user, username);
        return BlogJSONResult.build(status,"",null);
    }


}
